package com.xiaoc.bulldozer.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.xiaoc.bulldozer.common.AccountBalanceConstant;
import org.springframework.stereotype.Service;

import com.xiaoc.bulldozer.common.dao.AccountBalanceDetailMapper;
import com.xiaoc.bulldozer.common.dto.AccountBalanceDetailDto;
import com.xiaoc.bulldozer.common.dto.AccountBalanceDetailQueryDto;
import com.xiaoc.bulldozer.common.model.AccountBalance;
import com.xiaoc.bulldozer.common.model.AccountBalanceDetail;
import com.xiaoc.bulldozer.common.model.ConvertTradeOrder;
import com.xiaoc.bulldozer.common.service.AccountBalanceDetailService;

/**
 * AccountBalanceDetail Service 实现类
 *
 * @author Danfo Yam
 * @date 2018-08-24 15:47:58
 */
@Service("accountBalanceDetailService")
public class AccountBalanceDetailServiceImpl implements AccountBalanceDetailService {

    @Resource
    private AccountBalanceDetailMapper accountBalanceDetailMapper;

    @Override
    public Integer getAccountBalanceDetailCountByCondition(AccountBalanceDetailQueryDto accountBalanceDetailQueryDto) {
        return accountBalanceDetailMapper.getAccountBalanceDetailCountByCondition(accountBalanceDetailQueryDto);
    }

    @Override
    public List<AccountBalanceDetail> getAccountBalanceDetailListByCondition(AccountBalanceDetailQueryDto accountBalanceDetailQueryDto) {
        accountBalanceDetailQueryDto.setPage(accountBalanceDetailQueryDto.getPage());
        return accountBalanceDetailMapper.getAccountBalanceDetailListByCondition(accountBalanceDetailQueryDto);
    }

    @Override
    public AccountBalanceDetail getAccountBalanceDetailById(Integer id) {
        return accountBalanceDetailMapper.getAccountBalanceDetailById(id);
    }

    @Override
    public List<AccountBalanceDetail> getAllAccountBalanceDetailList() {
        return accountBalanceDetailMapper.getAllAccountBalanceDetailList();
    }

    @Override
    public void save(AccountBalanceDetail accountBalanceDetail) {
        accountBalanceDetailMapper.save(accountBalanceDetail);
    }

    @Override
    public void update(AccountBalanceDetail accountBalanceDetail) {
        accountBalanceDetailMapper.update(accountBalanceDetail);
    }

    @Override
    public void deleteById(Integer id) {
        accountBalanceDetailMapper.deleteById(id);
    }

    @Override
    public int getCountByTypeAndTime(Integer userId, int type, long startTime) {
        return accountBalanceDetailMapper.getCountByTypeAndTime(userId, type, startTime);
    }

    @Override
    public List<AccountBalanceDetailDto> getRecentCashAwardList(Integer limit) {
        return accountBalanceDetailMapper.getRecentCashAwardList(AccountBalanceConstant.ACCOUNT_BALANCE_TYPE_CASH, AccountBalanceConstant.AccountBalanceDetailConstant.TYPE_CASH_IN, limit);
    }

    @Override
    public void createAccountBalanceDetail(AccountBalance accountBalance, ConvertTradeOrder convertTradeOrder) {

        if (convertTradeOrder.getTradeFee() == 0) {
            return;
        }

        long currentTime = System.currentTimeMillis();

        AccountBalanceDetail detail = new AccountBalanceDetail();
        detail.setAccountId(accountBalance.getAccountId());
        detail.setAmount(convertTradeOrder.getTradeFee());
        detail.setBalanceId(accountBalance.getId());
        detail.setBalanceType(accountBalance.getType());
//        detail.setBusinessId(businessId);
        detail.setConvertTradeOrderId(convertTradeOrder.getId());
        detail.setCreateTime(currentTime);
        detail.setType(convertTradeOrder.getOrderType());
        detail.setUpdateTime(currentTime);
        detail.setUserId(accountBalance.getUserId());

        this.save(detail);

//        if (status == "AccountConstant.STATUS_COMPLETED") {
//            try {
//                weixinUserStatisticsService.addStatistics(userId, type, fee);
//            } catch (Throwable t) {
//                logger.error("add statistics failed", t);
//            }
//        }
    }

}
